package com.todo.sight.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.sight.infrastructure.entity.condition.SightReportCondition;
import com.todo.sight.infrastructure.entity.po.SightReportPO;
import com.todo.sight.infrastructure.mapper.SightReportMapper;
import com.todo.sight.infrastructure.repository.SightReportRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * (SightReport)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("sightReportRepository")
public class SightReportRepositoryImpl implements SightReportRepository {
    @Resource
    private SightReportMapper sightReportMapper;


    @Override
    public Boolean insertSightReport(SightReportPO sightReportPO) {
        if (sightReportPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(sightReportPO)) {
            return false;
        }
        return sightReportMapper.insertSightReportPO(sightReportPO) > 0;
    }

    @Override
    public Boolean updateSightReportById(SightReportPO sightReportPO) {
        if (sightReportPO == null || sightReportPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(sightReportPO)) {
            return false;
        }
        return sightReportMapper.updateSightReportPOById(sightReportPO) > 0;
    }


    @Override
    public SightReportPO querySightReportById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return sightReportMapper.querySightReportPOById(id);
    }

    @Override
    public Boolean removeSightReportById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return sightReportMapper.removeSightReportPOById(id) > 0;
    }

    @Override
    public PageResult<SightReportPO> querySightReportByCondition(SightReportCondition sightReportCondition) {
        return null;
    }

    private boolean baseFieldCheck(SightReportPO sightReportPO) {
        if (sightReportPO == null) {
            return false;
        }
        if (sightReportPO.getUserId() == null || sightReportPO.getEscalationType() == null || sightReportPO.getDesc() == null) {
            log.warn("SightReportRepository#baseFieldCheck 核心字段校验失败，默认操作失败 sightReportPO：【{}】", JSONObject.toJSONString(sightReportPO));
            return false;
        }
        return true;
    }
}
